package com.ruoyi.web.service.imp;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.ip.AddressUtils;
import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.SysLogininfor;
import com.ruoyi.system.service.ISysLogininforService;
import com.ruoyi.web.Vo.wx.WxUserVo;
import com.ruoyi.web.domain.WxUser;
import com.ruoyi.web.domain.WxVo.WxInfo;
import com.ruoyi.web.service.WxLoginService;
import com.ruoyi.web.service.WxUserService;
import com.ruoyi.web.utils.WxUtils;
import eu.bitwalker.useragentutils.UserAgent;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* @Project: serve
* @Package com.ruoyi.web.service.imp
* @Description: TODO(deng )
* @author : GuitarXiaohe
* @date Date : 2023年10月27日 09:47
* @version V1.0
*/

 @Service
public class WxLoginServiceImp implements WxLoginService {

@Resource
private WxUtils wxUtils;

 @Resource
 private WxUserService wxUserService;

 @Resource
 private TokenService tokenService;


 @Resource
 private ISysLogininforService iSysLogininforService;

 /**
  * 微信登录验证
  * @return  结果
  */

 public AjaxResult wxLogin(WxInfo wxInfo){

  WxUserVo wxUser = new WxUserVo();
 AjaxResult ajax = AjaxResult.success();
 // 通过code 获取openid
 String openId = wxUtils.getOpenId(wxInfo.getCode());
        wxUser.setOpenId(openId);
 /**
  * 判断wx用户表是否存在该openid 存在登录 否则注册登录
  */
 WxUser wxUserInfo = wxUserService.getOpenId(openId);
 // 注册
        if (wxUserInfo == null) {
//            wxUserService.insertWxUser(wxUser);
  return AjaxResult.success("请注册信息");
 }

 // 登录
 LoginUser loginUser = new LoginUser();
 SysUser sysUser = new SysUser();
        loginUser.setOpenid(openId);
        sysUser.setUserName(wxUserInfo.getName());
        sysUser.setPhonenumber(wxUserInfo.getPhone());
        loginUser.setUser(sysUser);
 // 记录登录
// wxLogininfor(wxUserInfo.getName(),"0");
        ajax.put(Constants.TOKEN, tokenService.createToken(loginUser));



        return AjaxResult.success(ajax);
}


 public AjaxResult wr(WxInfo wxInfo) throws Exception {
  WxUser wxUser = new WxUser();
  // 通过code 获取openid
  String openId = wxUtils.getOpenId(wxInfo.getCode());


  // 判断是否存在名称
  if (wxInfo.getName() == null) {
   return AjaxResult.error("请填写名称");
  }

  WxUser isName = wxUserService.getName(wxInfo.getName());
  WxUser isPhone = wxUserService.getPhone(wxInfo.getPhone());

  // 判断是否存在名称
  if (isName != null) {
   return AjaxResult.error("该名称已存在");
  }

  // 判断是否存在手机号
  if (isPhone != null) {
   return AjaxResult.error("该手机号已存在");
  }

  Pattern kong = Pattern.compile("\\s");//\\s匹配空格符、制表符、换行符和回车符
  Matcher kongRe = kong.matcher(wxInfo.getName());
  if (kongRe.find()) {
   return AjaxResult.error("名称不允许出现空格！");
  }

  String regex = "^[\\u4E00-\\u9FA5A-Za-z]+$";
  Pattern pattern = Pattern.compile(regex);
  Matcher matcher = pattern.matcher(wxInfo.getName());
  if (!matcher.find()) {
   return AjaxResult.error("只允许出现中文、字母的名称！");
  }




  wxUser.setOpenId(openId);
  wxUser.setName(wxInfo.getName().trim());
  wxUser.setPhone(wxInfo.getPhone());
  wxUser.setAvatar(wxInfo.getAvatar());
  int userId =   wxUserService.insertWxUser(wxUser);


  AjaxResult ajax = AjaxResult.success();
  // 登录
  LoginUser loginUser = new LoginUser();
  SysUser sysUser = new SysUser();
  sysUser.setPhonenumber(wxInfo.getPhone());
  loginUser.setOpenid(openId);
  loginUser.setUser(sysUser);
  // 记录登录
//  wxLogininfor(wxInfo.getName(),"0");
  ajax.put(Constants.TOKEN, tokenService.createToken(loginUser));
  return AjaxResult.success("注册成功",ajax);
 }




 /**
  * 记录登录信息
  *
  * @param userName state
  */
 public void wxLogininfor(String userName,String state){
  // 记录登录日志
  final String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
  String address = AddressUtils.getRealAddressByIP(ip);


  final UserAgent userAgent = UserAgent.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent"));

  // 获取客户端操作系统
  String os = userAgent.getOperatingSystem().getName();
  // 获取客户端浏览器
  String browser = userAgent.getBrowser().getName();
  // 封装对象
  SysLogininfor logininfor = new SysLogininfor();
  logininfor.setUserName(userName);
  logininfor.setIpaddr(ip);
  logininfor.setLoginLocation(address);
  logininfor.setBrowser(browser);
  logininfor.setOs(os);
  logininfor.setMsg("登录成功");
  logininfor.setStatus(state);
  iSysLogininforService.insertLogininfor(logininfor);
 }

}